package Stack;

/**
 * Created by sunwei on 2017/10/10
 * Time:9:27
 */
public class LinkList2 {
    private Link2 first;

    public LinkList2() {
        first = null;
    }

    public boolean isEmpty(){
        return (first==null);
    }

    /*
    插入链表节点，将新插入的链表节点后面指向原来的first节点，
     */
    public void InsertFirst(Double dd){
        Link2 newLink2= new Link2(dd);
        newLink2.next=first;
        first = newLink2;
    }
    /*
    删除节点
     */
    public double deleteFirst(){
        Link2 temp = first;
        first = first.next;
        return temp.dData;
    }

    /*
   查找特定关键词的节点
    */
    public Link2 FindLink(int key){
        Link2 current = first;
        while (current.dData!=key){
            if (current.next==null) {
                return null;
            }else {
                current = current.next;
            }
        }
        return current;
    }

    /*
    删除含有特定关键词的节点
     */
    public Link2 delete(int key){
        Link2 current = first;
        Link2 previous = first;
        if (current.dData!=key) {
            if (current.next==null) {
                return null;
            }else {
                previous = current;
                current = current.next;
            }
            if (current==first) {
                first = first.next;
            }else {
                previous.next = current.next;
            }
        }
        return current;
    }
    /*
    遍历显示节点
     */
    public void displayList(){
        System.out.print("List(first-->last): ");
        Link2 current = first;
        while (current.next!=null){
            current.displayLink();
            current = current.next;
        }
        System.out.println(" ");
    }

}
